package com.silence.gankio.widget

import android.annotation.SuppressLint
import android.content.Context
import android.os.Build
import android.support.v7.widget.Toolbar
import android.util.AttributeSet
import com.silence.gankio.R
import com.silence.gankio.util.StatusBarUtils
import android.util.TypedValue
import android.annotation.TargetApi


class BaseToolBar : Toolbar {

    var mFitStatusBar: Boolean
    private var mPaddingTop: Int
    private var mToolbarHeight: Int = 0
    private var mStatusBarH: Int = 0
    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        val typedArray = context!!.obtainStyledAttributes(attrs, R.styleable.BaseToolBar)
        mFitStatusBar = typedArray.getBoolean(R.styleable.BaseToolBar_fit_status_bar, false)
        val defaultBg = typedArray.getBoolean(R.styleable.BaseToolBar_default_bg, true)
        typedArray.recycle()
        if (defaultBg) {
            setDefaultBg()
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            elevation = context.resources.getDimensionPixelSize(R.dimen.dp_8).toFloat()
        }
        mPaddingTop = paddingTop
        init()
    }

    private fun init() {
        mToolbarHeight = resources.getDimensionPixelSize(R.dimen.toolbar_height)
        mStatusBarH = StatusBarUtils.getStatusBarH(context)
        val contentInset = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16F, resources.displayMetrics).toInt()
        setContentInsetsAbsolute(contentInset, contentInset)
        setContentInsetsRelative(contentInset, contentInset)


    }

    //适配透明状态栏
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (mFitStatusBar) {
                setMeasuredDimension(measuredWidth, mToolbarHeight + mStatusBarH)
                setPadding(paddingLeft, paddingTop + mStatusBarH, paddingRight, paddingBottom)
            } else {
                setMeasuredDimension(measuredWidth, mToolbarHeight)
                setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom)
            }
        }
    }

    /**
     * 适配透明状态栏
     *
     * @param fitStatusBar
     */
    @TargetApi(value = Build.VERSION_CODES.KITKAT)
    fun setFitStatusBar(fitStatusBar: Boolean) {
        this.mFitStatusBar = fitStatusBar
        requestLayout()
    }

    @SuppressLint("ResourceType")
    fun setDefaultBg(): BaseToolBar {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) setBackgroundColor(context.getColor(R.color.toolbar_color)) else setBackgroundColor(context.resources.getColor(R.color.toolbar_color))
        return this
    }

    fun setBackGroundAlpha(alpha: Float) {
        val background = background
        if (null != background) {
            background.mutate().alpha = (alpha * 255).toInt()
        }
    }

    /**
     * 是否是白色背景
     *
     * @param isDarkBg
     */
    fun isLightBackGround(isDarkBg: Boolean) {
        isSelected = isDarkBg
    }

}